#include <rtdef.h>
#include <rtthread.h>
#include "power.h"
#include "hc32f460_clk.h"
#include "cpuHc32f460.h"
#include "hc32f460_adc.h"
#include "hc32f460_pwc.h"
#include "hc32_ll_pwc.h"
#include "hc32_ll_adc.h"
#include "hc32f460_gpio.h"

//port_init_t TPower_Config[] =
//{
//	//PORT		   PIN    FUNC      Mode         LTE     INTE   INVE    PUU     DRV         NOD          BFE    Level
//        {.enPort = PortB, Pin00, Func_Gpio, Pin_Mode_Ana, Disable, Disable, Disable, Disable, Pin_Drv_M, Pin_OType_Od, Disable, GPIO_DEFAULT},  // 4.PB0   	5.2V掉电检测   
//        {.enPort = PortB, Pin01, Func_Gpio, Pin_Mode_Ana, Disable, Disable, Disable, Disable, Pin_Drv_M, Pin_OType_Od, Disable, GPIO_DEFAULT},  // 3. PC5 时钟电池检测  
//        {.enPort = PortC, Pin14, Func_Gpio, Pin_Mode_Out, Disable, Disable, Disable, Disable, Pin_Drv_M, Pin_OType_Cmos, Disable, HIGH}, // 14.PC4 5V电源
//        {.enPort = PortB, Pin02, Func_Gpio, Pin_Mode_Out, Disable, Disable, Disable, Disable, Pin_Drv_M, Pin_OType_Cmos, Disable, LOW},// 29.PE10 3.3V电源
//        {.enPort = PortA, Pin15, Func_Gpio, Pin_Mode_Out, Disable, Disable, Disable, Disable, Pin_Drv_M, Pin_OType_Cmos, Disable, LOW},// 29.PA15 8306电源
//        {.enPort = PortC, Pin10, Func_Gpio, Pin_Mode_Out, Disable, Disable, Disable, Disable, Pin_Drv_M, Pin_OType_Cmos, Disable, HIGH},// 16.PC10 8306复位
//};

//-----------------------------------------------
//函数功能: 初始化ADC(保护电流)
//
//参数:		Type：采样点数
//			
//返回值:  	无
//
//备注: A/B/C三相电流 + 剩余电流
//-----------------------------------------------
static void InitADC(void)
{

}

// ADC 设备初始化函数
static rt_err_t adc_init(struct rt_device *dev)
{
    rt_adc_device *adc_dev = (rt_adc_device *)dev;
    
//    InitGPIO(&TPower_Config[0],sizeof(TPower_Config)/sizeof(port_init_t));
//    adc_dev->hadc = M4_ADC1; //选择通道

    return RT_EOK;
}

// ADC 控制函数（设置通道、读取数据等）
static rt_err_t adc_control(struct rt_device *dev, int cmd, void *args)
{
}

struct rt_device_ops pops=
{
    .init    = adc_init,
    .open    = RT_NULL,
    .close   = RT_NULL,  // 不需要关闭操作
    .read    = RT_NULL,  // 使用 control 接口读取
    .write   = RT_NULL,  // 使用 control 接口写入
    .control = adc_control,
};

int Power_gpio_device_register(void)
{
    rt_adc_device *adc_device;

    // 1. 分配内存
    adc_device = (rt_adc_device *)rt_malloc(sizeof(rt_adc_device));
    if (adc_device == RT_NULL) 
    {
        rt_kprintf("Error: Failed to allocate memory for adc_device!\n");
        return -RT_ENOMEM;
    }

    memset(&adc_device->parent, 0, sizeof(struct rt_device));
    
    /* 设置设备操作函数 */
    adc_device->parent.type    = RT_Device_Class_Miscellaneous;
    adc_device->parent.ops  = &pops;
    adc_device->parent.user_data = adc_device;
    
    /* 注册设备到系统 */
    int result = rt_device_register(&adc_device->parent, "power_gpio", RT_DEVICE_FLAG_RDWR);
    
    if(result != RT_EOK)
    {
        rt_free(adc_device);
    }
    return RT_EOK;
}
// 设备注销函数
void Power_gpio_device_unregister(void)
{
    rt_device_t dev = rt_device_find("power_gpio");
    if (dev)
    {
        rt_device_unregister(dev);
        rt_free(dev->user_data);
    }
}

/* 导出自动初始化函数（在系统启动时自动执行） */
INIT_DEVICE_EXPORT(Power_gpio_device_register);